package com.cande.loki.exercise.streamdemo;

import com.google.common.collect.Maps;

import javax.swing.text.html.Option;
import java.sql.Driver;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ｗｇｆ
 * @date 2021/7/12 15:53
 * @des
 */
public class TestDemo {
    public static void ctt() throws Exception{
        try {
            tt();
        }catch (Exception e){
            System.out.println(1);
        }
    }
    public static void main(String[] args) throws Exception {

        ServiceLoader<Driver> loader  = ServiceLoader.load(Driver.class);
        System.out.println(ClassLoader.getSystemClassLoader());

        for (Driver driver : loader) {
            System.out.println("diverName :" +driver.getClass().getName()+" , loadDriver's Loader: "+driver.getClass().getClassLoader());
        }
        System.out.println("当前线程上下文类加载器 : "+Thread.currentThread().getContextClassLoader());
        System.out.println("加载ServiceLoader的类加载 : " +loader.getClass().getClassLoader());



        System.out.println(ClassLoader.getSystemClassLoader());

        Stu ssss= new Stu();
        ssss.setNum(1);
        Integer ageage = ssss.getNum();

        ssss.setNum(2);
        ageage =3;
        System.out.println(ageage);

        int xx=0;
        int yy=xx;
        yy=6;
        System.out.println(xx);



        int[] ghgh = {0,1,2,3};
        for(int i: ghgh){
            try {
                if (i==2){
                    continue;
                }
                System.out.println(i);
            }catch (Exception e){

            }
        }


int ll = 1;
        boolean ckk =  Pattern.matches("^([1-9]|1[0-2])$", ll+"");
        boolean ckk1 =  Pattern.matches("^([1-9]|1[0-2])$",12+"");
        boolean ckk2 =  Pattern.matches("^([1-9]|1[0-2])$",13+"");
        boolean ckk3=  Pattern.matches("^([1-9]|1[0-2])$",01+"");
        Stu H=new Stu("1","1",1);

        Map<Integer,Stu> cc = Maps.newHashMap();
        cc.put(1,H);

        String DASD = Optional.ofNullable(cc.get(1)).map(e->e.getName()).orElse("");
        try{
ctt();

        }catch (RuntimeException e){
            System.out.println(2);
        }catch (Exception E){

        }

        List<Stu> oo1 = new ArrayList<>();
        Optional<Stu> cc1 = oo1.stream().max(Comparator.comparing(e->e.age));
        Stu kj = new Stu();
//        if (kj.getNum()==1){
//
//        }
//        if (1==kj.getNum()){
//
//        }
       Integer KKK = cc1.map(Stu::getNum).orElse(2);
        System.out.println(KKK);

        List<Stu> oo = new ArrayList<>();
        oo.add(new Stu("2","2"));
        Stu kk = null;
        kk = oo.get(0);
//生成流
        // iterate
        Stream.iterate(10, e -> e + 1).limit(10).forEach(System.out::println);
        // of
        Stream.of(1, 2, 3, 5).forEach(System.out::println);
        Stream.of("taobao".split("")).forEach(System.out::println);
        // generate
        Stream.generate(Math::random).limit(2).forEach(System.out::println);
//中间操作
        // map
       Stream.of("a","b","c","d").filter(e->e.startsWith("a")).map(e->e.substring(1)).forEach(System.out::println);
        //filter
        List<Integer> lists = Stream.of(1,3,4,5).filter(e->e>2).collect(Collectors.toList());
        //flatMap() 扁平化映射，它具体的操作是将多个stream连接成一个stream，这个操作是针对类似多维数组的，比如集合里面包含集合，相当于降维作用。
        List<Integer> num1 = Arrays.asList(1, 2, 3);
        List<Integer> num2 = Arrays.asList(4, 5, 6);
        List<Integer> num3 = Arrays.asList(7, 8, 9);
        List<List<Integer>> lists1 = Arrays.asList(num1,num2,num3);
        List<Integer> res1= lists1.stream().flatMap(e->e.stream()).collect(Collectors.toList());
        // sorted

        // sorted:自然顺序排序
        List<Integer> nums = Arrays.asList(1, 3, 5, 6, 8, 2);
        List<Integer> sortedNum = nums.stream().sorted().collect(Collectors.toList());
        System.out.println(sortedNum);

        // sorted:降序排序
        List<Integer> sortedNum2 = nums.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(sortedNum2);

        // sorted:使用Comparator
        List<Integer> sortedNums3 = nums.stream().sorted(Comparator.comparing(n -> n)).collect(Collectors.toList());
        System.out.println(sortedNums3);

        // 不用stream直接顺序排序
        nums.sort(Comparator.comparing(Integer::intValue));
        System.out.println(nums);

        //不用stream直接降序排序
        nums.sort(Comparator.comparing(Integer::intValue).reversed());
        System.out.println(nums);

        //peek

        String[] arr = new String[]{ "a","b","c"};
        List<String> list =   Arrays.stream(arr).peek(e->e.toUpperCase()).collect(Collectors.toList());
        System.out.println(list);

        List<Stu> stus = new ArrayList<>();
        Stu stu1 = new Stu("11","aa",1);
        Stu stu2 = new Stu("22","bb",2);
        stus.add(stu2);
        stus.add(stu1);
        String cccccc = stus.stream().distinct().map(e->e.getNum().toString()).collect(Collectors.joining(","));

        Map<Boolean,List<Stu>>  heihei= stus.stream().collect(Collectors.groupingBy(e->e.getNum().equals(1)));

        List<Stu> stus1 =  stus.stream().peek(e->e.setAge("3333333")).collect(Collectors.toList());
        System.out.println(stus1);
//终止操作
        //allMatch
        boolean res2 = stus.stream().allMatch(e->e.age.equals("11"));
        System.out.println(res2);
        //anyMatch
        boolean res3 = stus.stream().anyMatch(e->e.age.equals("11"));
        System.out.println(res3);
        //noneMatch
        boolean res4 = stus.stream().noneMatch(e->e.age.equals("11"));
        System.out.println(res4);
        //findFirst
        Optional<Stu> res5 = stus.stream().findFirst();
        System.out.println(res5.get().age);
        //findAny
        Optional<Stu> res6 = stus.stream().findAny();
        System.out.println(res6.get().age);
        //count
        long res7 = stus.stream().count();
        //max
        Optional<Stu> res8 = stus.stream().max(Comparator.comparingInt(Stu::getNum));
        //min
        Optional<Stu> res9 = stus.stream().min(Comparator.comparingInt(Stu::getNum));
        //reduce
        Optional<Integer> res10 =  stus.stream().map(e->e.getNum()).reduce((e1,e2)-> e1+e2);
        System.out.println(res10.get());

        Optional res11 = Stream.of(1,2,3,4).reduce((acc,item)->acc+item);
        System.out.println(res11);


        Integer res12 = Stream.of(1,2,3,4).reduce(10,(acc,item)-> acc+item);
        System.out.println(res12);
        List<List<Integer>> listss = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        List<Integer> list2 = new ArrayList<>();
        list2.add(2);
//        List<Integer> list3 = new ArrayList<>();
//        list3.add(3);
        listss.add(list1);
        listss.add(list2);
//        listss.add(list3);

//        int rel = listss.stream().reduce(1, (a,b)->a + b.get(0), (x,y)->x+y);   结果 4
        //并行流 会把初始值计算两边  ，x y  是不同线程的结果 并行计算下 合并各个线程的计算结果
        int rel = listss.parallelStream().reduce(1, (a,b)->a + b.get(0), (x,y)->x+y); // 结果 5
        System.out.println(rel);

        //collect LIST根据多个字段排序
        List<Stu> testList = new ArrayList<>();
        Stu A = new Stu();

        Stu B = new Stu();
        Stu C = new Stu();
        Stu D = new Stu();
        D.setNum(2);
        D.setNum1(3);

        A.setAge("c");
        B.setAge("e");
        C.setAge("a");
        D.setAge("d");
        A.setNum(1);
        B.setNum(2);
        C.setNum(2);

        A.setNum1(1);
        B.setNum1(2);
        C.setNum1(8);
        testList.add(A);
        testList.add(B);
        testList.add(C);
        testList.add(D);
        //Comparator.comparing根据多个字段
//        testList = testList.stream().sorted(Comparator.comparing(Stu::getNum).thenComparing(Stu::getNum1).reversed()).collect(Collectors.toList());
        //sort重写排序方法，如果a>b 返回大于0 就是升序 小于零就是降序
//        testList = testList.stream().sorted((a,b)->{
//            if (a.getNum1()>b.getNum1()){
//                return -1;
//            }
//         return 1;
//        }).collect(Collectors.toList());

       testList.stream().sorted(Comparator.comparing(Stu::getNum,(a,b)->{
           if (a>b){
               return -1;
           }
           return 1;
       }).thenComparing(Stu::getAge)).collect(Collectors.toList()).forEach(e-> {System.out.println("num1:"+e.getNum1()+" age:"+e.getAge() +"    num:"+e.getNum());});
        System.out.println("hhh");
    }


    public static void tt() throws Exception{
        throw new RuntimeException();
    }
    static class Stu{
        private String name;
        private String age;
        private Integer num;
        private Integer num1;
        public Stu() {

        }
        public Stu(String name, String age, Integer num) {
            this.name = name;
            this.age = age;
            this.num = num;
        }

        public Stu(String name, String age) {
            this.name = name;
            this.age = age;
        }

        public Integer getNum1() {
            return num1;
        }

        public void setNum1(Integer num1) {
            this.num1 = num1;
        }

        public Integer getNum() {
            return num;
        }

        public void setNum(Integer num) {
            this.num = num;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }
}
